// src/contrllor/users.js
class Users {
    index(req, res) {
        req.db.query('select `id`,`username`,`phone`,`age`,`gender`,`email`,`desc` from `users`', (error, result) => {
            // console.log(error)
            res.send(result)
        })
    }

    add(req, res) {
        let username = req.body.username,
        phone = req.body.phone,
        age = req.body.age,
        gender = req.body.gender,
        email = req.body.email,
        desc = req.body.desc;
        req.db.query('insert into `users` set `username`="'+username+
        '",`phone`="'+phone+
        '",`age`="'+age+
        '",`gender`="'+gender+
        '",`email`="'+email+
        '",`desc`="'+desc+'"', (error, result) => {
            // console.log(error)
            res.send(result)
        })
    }

    edit(req, res) {
        let username = req.body.username,
            phone = req.body.phone,
            age = req.body.age,
            gender = req.body.gender,
            email = req.body.email,
            desc = req.body.desc,
            id = req.body.id;
        req.db.query('update `users` set `username`="'+username+
            '",`phone`="'+phone+
            '", `age`="' + age +
            '",`gender`="' + gender +
            '",`email`="' + email +
            '",`desc`="' + desc +'" where `id`="'+id+'"', (error, result) => {
            res.send(result)
        })
    }

    del(req, res) {
        let id = req.query.id
        req.db.query('delete from `users` where `id`="'+id+'"', (error, result) => {
            res.send(result)
        })
    }

    find(req,res){
        let id = req.query.id;
        req.db.query('select `id`,`username`,`phone`,`age`,`gender`,`email`,`desc` from `users` where `id`="'+id+'"', (error, result) => {
            res.send(result[0]);
        });
    }
    say(req, res) {
        res.send('hello')
    }
}

module.exports = new Users()